package misc;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;

import static misc.Utils.GSON;

public class Folder {
    private final String path;

    public Folder(String path) {
        this.path = path;

        File file = new File(path);
        if (!file.isDirectory())
            file.mkdirs();
    }

    File get(String name) {
        return new File(path + "/" + name);
    }

    String getString(String name) throws IOException {
        return Files.toString(get(name), Charsets.UTF_8);
    }

    public <E> E fromJson(String name, Class<E> theClass) throws IOException {
        return GSON.fromJson(getString(name + ".json"), theClass);
    }

    public <E> void toJson(String name, E object) throws IOException {
        Files.write(GSON.toJson(object), get(name + ".json"), Charsets.UTF_8);
    }
}
